/**
 * 管理初始化
 */
var ClmRoom = {
    id: "ClmRoomTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ClmRoom.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        	{title: '课堂编号', field: 'number', visible: false, align: 'center', valign: 'middle'},
            {title: '课堂ID', field: 'roomId', visible: false, align: 'center', valign: 'middle'},
            {title: '课堂状态', field: 'invalidDate', visible: false, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '即将开始/正在直播'; 
                yourtime = value.replace("-","/");//替换字符，变成标准格式  
                var d2=new Date();//取今天的日期  
                var d1 = new Date(Date.parse(yourtime));  
                if(d1<d2){  
                	 text = "已结束";  
                } 
                return text;
            }},
            {title: '课堂名称', field: 'subject', visible: true, align: 'center', valign: 'middle'},
            {title: '开始时间', field: 'startDate', visible: true, align: 'center', valign: 'middle'},
            {title: '课程科目', field: 'subjectName', visible: false, align: 'center', valign: 'middle'},
            {title: '教师', field: 'teacherName', visible: true, align: 'center', valign: 'middle'},
            {title: '教职区域', field: 'teachArea', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = ''; 
                text=row.orgProvinceName+row.orgCityName+row.orgAreaName
                return text;
            }},
            {title: '就职学校', field: 'schoolName', visible: true, align: 'center', valign: 'middle'},
            {title: '课程类型', field: 'liveType', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '-';
                if (value == "home_and_school") {
                    text = "家校互通";
                } else if (value == "live_teach") {
                    text = "直播教研";
                } else if (value == "more_school") {
                    text = "一校带多校";
                } 
                return text;
            }},
            {title: '公开范围', field: 'publicScope', visible: true, align: 'center', valign: 'middle',formatter: function (value, row, index){
                var text = '-';
                if (value == "school_public") {
                    text = "本校公开";
                } else if (value == "grade_public") {
                    text = "年级公开";
                } else if (value == "class_public") {
                    text = "班级公开";
                } else if (value == "area_public") {
                    text = "本区（县）公开";
                }else if (value == "all_public") {
                    text = "完全公开";
                } 
                return text;
            }},
            {title: '站点id', field: 'siteId', visible: false, align: 'center', valign: 'middle'},
            {title: '老师ID', field: 'teacherId', visible: false, align: 'center', valign: 'middle'},
            {title: '所属学校ID(第三方获取)', field: 'schoolId', visible: false, align: 'center', valign: 'middle'},
            {title: '所属学段ID(第三方获取)', field: 'stageId', visible: false, align: 'center', valign: 'middle'},
            {title: '所属年级ID(第三方获取)', field: 'gradeId', visible: false, align: 'center', valign: 'middle'},
            {title: '年级字典表id', field: 'dictGradeId', visible: false, align: 'center', valign: 'middle'},
            {title: '所属班级ID(第三方获取)', field: 'classId', visible: false, align: 'center', valign: 'middle'},
            {title: '所属科目ID(第三方获取)', field: 'subjectId', visible: false, align: 'center', valign: 'middle'},
            {title: '科目code', field: 'courseCode', visible: false, align: 'center', valign: 'middle'},
            {title: '老师口令(长度6-15)', field: 'teacherToken', visible: false, align: 'center', valign: 'middle'},
            {title: 'web端学生口令(长度最大15)', field: 'studentToken', visible: false, align: 'center', valign: 'middle'},
            {title: '客户端学生口令', field: 'studentClientToken', visible: false, align: 'center', valign: 'middle'},
            {title: '失效时间', field: 'invalidDate', visible: false, align: 'center', valign: 'middle'},
            {title: '助教加入口令', field: 'assistantToken', visible: false, align: 'center', valign: 'middle'},
            {title: '老师介绍', field: 'speakerInfo', visible: false, align: 'center', valign: 'middle'},
            {title: '课程介绍', field: 'scheduleInfo', visible: false, align: 'center', valign: 'middle'},
            {title: '是否支持web端学生加入,默认为true', field: 'webJoin', visible: false, align: 'center', valign: 'middle'},
            {title: '是否支持客户端学生加入', field: 'clientJoin', visible: false, align: 'center', valign: 'middle'},
            {title: '', field: 'teacherJoinUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '学员加入url', field: 'studentJoinUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '课堂介绍', field: 'description', visible: false, align: 'center', valign: 'middle'},
            {title: '课堂时长', field: 'duration', visible: false, align: 'center', valign: 'middle'},
            {title: 'web端学生界面设置(1是三分屏、2是文档视频、3是两分屏、4是互动增加)', field: 'uiMode', visible: false, align: 'center', valign: 'middle'},
            {title: '三分屏颜色选择(blue、default、green，默认是default)', field: 'uiColor', visible: false, align: 'center', valign: 'middle'},
            {title: '0:大讲堂，1小班课。默认值0', field: 'scene', visible: false, align: 'center', valign: 'middle'},
            {title: '是否显示小窗口,默认为false', field: 'uiWindow', visible: false, align: 'center', valign: 'middle'},
            {title: '是否视频为主，默认为false', field: 'uiVideo', visible: false, align: 'center', valign: 'middle'},
            {title: '手机浏览器类型', field: 'mobileMode', visible: false, align: 'center', valign: 'middle'},
            {title: '是否允许web升级到客户端', field: 'upgrade', visible: false, align: 'center', valign: 'middle'},
            {title: '密码是否加密', field: 'sec', visible: false, align: 'center', valign: 'middle'},
            {title: '是否实时', field: 'realtime', visible: false, align: 'center', valign: 'middle'},
            {title: '课堂占用发数', field: 'maxAttendees', visible: true, align: 'center', valign: 'middle', footerFormatter: function (value) {
            	var count = 0; 
            	for (var i in value) { 
            		var tmpNum=parseInt(value[i].maxAttendees)
            		if(isNaN(tmpNum)){
            			tmpNum = 0;
            	    }
            		count += tmpNum; 
            		} 
            	return "总计："+count; 
            }},
            {title: '创建者', field: 'createUserId', visible: false, align: 'center', valign: 'middle'},
            {title: '创建时间', field: 'createDate', visible: false, align: 'center', valign: 'middle'},
            {title: '是否要求登陆，1要求，0不要求 默认1', field: 'beLogin', visible: false, align: 'center', valign: 'middle'},
            {title: '上传封面图片的路径', field: 'imageUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '年级名称', field: 'gradeName', visible: false, align: 'center', valign: 'middle'},
            {title: '班级名称', field: 'className', visible: false, align: 'center', valign: 'middle'},
            {title: '是否删除,true(1)删除，false(0)正常', field: 'beDeleted', visible: false, align: 'center', valign: 'middle'},
            {title: '冗余字段，创建课堂时给第三方发送的课堂名称参数，程序中后面加上了时间戳', field: 'thirdSubject', visible: false, align: 'center', valign: 'middle'},
            {title: '录制件审核状态：  0-立即公开，1-审核后公开', field: 'beCheck', visible: false, align: 'center', valign: 'middle'},
            {title: '上传的录制件url链接', field: 'upUrl', visible: false, align: 'center', valign: 'middle'},
            {title: '正在观看人数', field: 'lookNum', visible: true, align: 'center', valign: 'middle', footerFormatter: function (value) {
            	var count = 0; 
            	for (var i in value) { 
            		var tmpNum=parseInt(value[i].lookNum)
            		if(isNaN(tmpNum)){
            			tmpNum = 0;
            	    }
            		count += tmpNum; 
            		} 
            	return "总计："+count; 
            }}
    ];
};


/**
 * 检查是否选中
 */
ClmRoom.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ClmRoom.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加
 */
ClmRoom.openAddClmRoom = function () {
    var index = layer.open({
        type: 2,
        title: '添加',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/clmRoom/clmRoom_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看详情
 */
ClmRoom.openClmRoomDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '详情',
            area: ['850px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/clmRoom/clmRoom_update/' + ClmRoom.seItem.roomId
        });
        this.layerIndex = index;
    }
};

/**
 * 打开查看
 */
ClmRoom.openClmRoomLook = function () {
    if (this.check()) {
    	//window.location.href="https://www.baidu.com";
    	param={roomId : ClmRoom.seItem.roomId,siteId : ClmRoom.seItem.siteId}
    	//param={roomId : "9jdc3HKyZX"}
    	jQuery.getJSON(Feng.ctxPath + "/clmRoom/lookRoom",param,function(resultData){
    		resultData=jQuery.parseJSON(resultData);
            if(resultData.result==0){
                layer.alert(resultData.message,{
                  icon: 0,
				  btnAlign: 'c'
                });
					
            }else{
                 if(typeof(resultData.upUrl)!="undefined" && resultData.upUrl!=''){
					 window.open(resultData.upUrl);
				}else{
					window.open(resultData.lookUrl,'_blank','');
				}
            }

        });

    }
};

/**
 * 删除
 */
ClmRoom.delete = function () {
    if (this.check()) {
    	var operation = function(){
	        var ajax = new $ax(Feng.ctxPath + "/clmRoom/delete", function (data) {
	            Feng.success("删除成功!");
	            ClmRoom.table.refresh();
	        }, function (data) {
	            Feng.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("roomId",ClmRoom.seItem.roomId);
	        ajax.start();
	    };
	    Feng.confirm("是否删除?",operation);
    }
};

/**
 * 查询列表
 */
ClmRoom.search = function () {
    var queryData = {};
    queryData['teacherName'] = $("#teacherName").val();
    queryData['schoolId'] = $("#schoolId").val();
    queryData['orgProvinceCode'] = $("#orgProvinceCode").val();
    queryData['orgCityCode'] = $("#orgCityCode").val();
    queryData['orgAreaCode'] = $("#orgAreaCode").val();
    //queryData['roomId'] = $("#roomId").val();
    queryData['number'] = $("#number").val();
    queryData['subject'] = $("#subject").val();
    queryData['liveType'] = $("#liveType").val();
    queryData['publicScope'] = $("#publicScope").val();
    queryData['courseCode'] = $("#courseCode").val();
    queryData['startDate1'] = $("#startDate1").val();
    queryData['endDate1'] = $("#endDate1").val();
    queryData['roomStatus'] = $("#roomStatus").val();
    
    ClmRoom.table.refresh({query: queryData});
};

ClmRoom.resetSearch = function () {
    $("#teacherName").val("");
    $("#schoolId").val("");
    //$("#roomId").val("");
    $("#number").val("");
    $("#subject").val("");
    $("#liveType").val("");
    $("#publicScope").val("");
    $("#courseCode").val("");
    $("#startDate1").val("");
    $("#endDate1").val("");
    $("#roomStatus").val("");
    $("#orgProvinceCode").val("");
    $("#orgCityCode").val("");
    $("#orgAreaCode").val("");
    $("#schoolId").val("");
    ClmRoom.search();
}


/**
 * 获取市
 */
function getCityList(orgProvinceCode) {
        var ajax = new $ax(Feng.ctxPath + "/clmRoom/getCityList", function (resultData) {
        	jQuery("#orgCityCode").html("");
        	if(resultData instanceof Array){
        		jQuery("#orgCityCode").append("<option value='' ></option>");
                for (key in resultData){
                    var name=resultData[key].cityName;
                    var code=resultData[key].cityCode;
                     jQuery("#orgCityCode").append("<option value='"+code+"' >"+name+"</option>");
                }
                //getDiscList(resultData[0].orgCityCode);
			}else{
	             var name=resultData.cityName;
	             var code=resultData.cityCode;
				 jQuery("#orgCityCode").append("<option value='"+code+"' >"+name+"</option>");
			}
        	
        }, function (data) {
        	jQuery("#orgCityCode").append("<option></option>");
            jQuery("#orgAreaCode").html("<option></option>");
            jQuery("#schoolId").html("<option></option>");
        });
        ajax.set("provinceCode",orgProvinceCode);
        ajax.start();
};

/**
 * 获取区县
 */
function getDiscList(orgCityCode) {
        var ajax = new $ax(Feng.ctxPath + "/clmRoom/getDiscList", function (resultData) {
        	jQuery("#orgAreaCode").html("");
        	if(resultData instanceof Array){
        		jQuery("#orgAreaCode").append("<option value='' ></option>");
                for (key in resultData){
                    var name=resultData[key].discName;
                    var code=resultData[key].discCode;
                     jQuery("#orgAreaCode").append("<option value='"+code+"' >"+name+"</option>");
                }
               // getSchoolList(resultData[0].discCode,"2");
			}else{
	             var name=resultData.discName;
	             var code=resultData.discCode;
				 jQuery("#orgAreaCode").append("<option value='"+code+"' >"+name+"</option>");
			}
        	
        }, function (data) {
            jQuery("#orgAreaCode").html("<option></option>");
            jQuery("#schoolId").html("<option></option>");
        });
        ajax.set("cityCode",orgCityCode);
        ajax.start();
};

/**
 * 获取学校
 */
function getSchoolList(code,type) {
        var ajax = new $ax(Feng.ctxPath + "/clmRoom/getSchoolList", function (resultData) {
        	jQuery("#schoolId").html("");
        	if(resultData instanceof Array){
        		jQuery("#schoolId").append("<option value='' ></option>");
                for (key in resultData){
                    var name=resultData[key].schoolName;
                    var code=resultData[key].schoolId;
                     jQuery("#schoolId").append("<option value='"+code+"' >"+name+"</option>");
                }
			}else{
	             var name=resultData.schoolName;
	             var code=resultData.schoolId;
				 jQuery("#schoolId").append("<option value='"+code+"' >"+name+"</option>");
			}
        	
        }, function (data) {
            jQuery("#schoolId").html("<option></option>");
        });
        ajax.set("code",code);
        ajax.set("type",type);
        ajax.start();
};

//省变更事件
$("#orgProvinceCode").bind("change",function () { 
	getCityList(this.value)
	if(this.value!=""){
		getSchoolList(this.value,"0");
	}
	
 })
 
 //市变更事件
$("#orgCityCode").bind("change",function () { 
	getDiscList(this.value)
	if(this.value!=""){
		getSchoolList(this.value,"1");
	}
	
 })
 
 //区县变更事件
$("#orgAreaCode").bind("change",function () { 
	if(this.value!=""){
		getSchoolList(this.value,"2");
	}
	
 })



$(function () {
    var defaultColunms = ClmRoom.initColumn();
    var table = new BSTable(ClmRoom.id, "/mon/roomList", defaultColunms);
    table.setPaginationType("client");
    
    //var datas = "[{'ii':'aa','aa':'bb'},{'ii':'a55a','aa':'b55b'}]";
    
   /* var datas = "[{'ii':'aa','aa':'bb'},{'ii':'a55a','aa':'b55b'}]";
    $('#ClmRoomTable').bootstrapTable('insertRow', {
    	index:$('#ClmRoomTable').bootstrapTable('getOptions').totalRows,
    	row:datas
    	});
    $('#ClmRoomTable').bootstrapTable('mergeCells',{index:index, field:fieldName, colspan: colspan, rowspan: count});*/
    
    ClmRoom.table = table.init();
    $('#ClmRoomTable').bootstrapTable( {
		showFooter: true
	});
    
    //每隔5分自动刷新列表
    window.setTimeout(ClmRoom.resetSearch(),60000*5);
    
});
